//
//  LWOAuthViewController.m
//  1.28  weibo
//
//  Created by mac on 15/2/2.
//  Copyright (c) 2015年 mac. All rights reserved.
//

#import "LWOAuthViewController.h"
#import "SVProgressHUD.h"
#import "AFNetworking.h"
#import "MJExtension.h"
#import "LWAccount.h"
#import "LWNewfeatureVc.h"
#import "LWWelcomeVc.h"
#import "UIWindow+Extension.h"
#import "LWNetWorkTools.h"

#define LWAuthorizeUrl @"https://api.weibo.com/oauth2/authorize"
#define LWClientId @"3110138268"
#define LWRedirectUri @"http://www.baidu.com"
//#define LWAccessToken @"https://api.weibo.com/oauth2/access_token"
//#define LWClient_secret @"6bcddc2aabf782740435a5201a7eff4b"

@interface LWOAuthViewController () <UIWebViewDelegate>



@end

@implementation LWOAuthViewController

- (void)loadView{
    UIWebView *webView = [[UIWebView alloc] init];
    self.view = webView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    //1.添加导航栏的取消按钮
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"取消" style:UIBarButtonItemStylePlain target:self action:@selector(cancleClick)];
    //2.加载登陆界面
    [self logOAuthPage];
}

#pragma mark - 加载登陆界面
- (void)logOAuthPage{

    //1.拼接URL的路径
    NSString *urlString = [NSString stringWithFormat:@"%@?client_id=%@&redirect_uri=%@",LWAuthorizeUrl,LWClientId,LWRedirectUri];
    //2.创建URL
    NSURL *url= [NSURL URLWithString:urlString];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    //设置代理
    //2.加载网页
    UIWebView *webView = (UIWebView *)self.view;
    webView.delegate = self;
    [webView loadRequest:request];
}

#pragma mark - UIWebViewDelegate
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
    //1.获取完整的路径
    NSString *urlString = request.URL.absoluteString;
    //2.获取截取的范围
    NSRange range = [urlString rangeOfString:@"code="];
    if (range.location != NSNotFound) {
        NSInteger startIndex = range.location + range.length;
        NSString *code = [urlString substringFromIndex:startIndex];
        [self accessTokenWithCode:code];
        return NO;
    }
    return YES;
}
//根据已授权的request token获取令牌
- (void)accessTokenWithCode:(NSString *)code{
    
    [[LWNetWorkTools shareNetWorkTools] loadAccessTokenWithCode:code success:^(LWAccount *account) {
        
        if (account) {
            /**
             *  根据版本判断显示的界面
             */
            [[UIApplication sharedApplication].keyWindow chooseViewController];

        }
    
    } failure:^(NSError *error) {
        LWLog(@"eror = %@",error);
    }];
    
}

#pragma mark - 加载网页时调用的方法
- (void)webViewDidStartLoad:(UIWebView *)webView{
    [SVProgressHUD showWithStatus:@"正在拼命加载..." maskType:SVProgressHUDMaskTypeGradient];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView{

    [SVProgressHUD dismiss];
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{

    [SVProgressHUD dismiss];
}

- (void)cancleClick{

    [self dismissViewControllerAnimated:YES completion:nil];
}


@end
